package ddd6;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

public class work3{
    public static void main(String[] args) {
        String folderPath = "E:/code";
        File f = new File(folderPath);
        if (!f.exists() || !f.isDirectory()) {
            System.err.println("错误：路径不存在或不是文件夹！");
            return;
        }
        Map<String, Integer> fileTypeCount = new HashMap<>();
        countFiles(f, fileTypeCount);
        System.out.println("文件类型统计结果：");
        for (Map.Entry<String, Integer> entry : fileTypeCount.entrySet()) {
            System.out.println(entry.getKey() + entry.getValue() + " 个");
        }
    }
    private static void countFiles(File dir, Map<String, Integer> map) {
        File[] files = dir.listFiles();
        if (files == null) return;
        for (File file : files) {
            if (file.isDirectory()) {
                countFiles(file, map);
            } else {
                String fileName = file.getName();
                String fileType = " ";
                int dotIndex = fileName.lastIndexOf('.');
                if (dotIndex > 0) {
                    fileType = fileName.substring(dotIndex + 1).toLowerCase();
                }
                map.put(fileType, map.getOrDefault(fileType, 0) + 1);
            }
        }
    }
}
